package com.fita.admin.web.controller.coupon;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.content.CouponUseSearch;
import com.fita.admin.service.coupon.CouponService;
import com.fita.entity.coupon.Coupon;
import com.fita.entity.coupon.CouponStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 礼品券核销
 * @author Administrator
 *
 */
@Controller
@RequestMapping("coupon-use")
@MenuSetting("menu-coupon")
public class CouponUseController {

    private static final Logger logger = LoggerFactory.getLogger(CouponUseController.class);


    @Autowired
    private CouponService<Coupon> couponService;


    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-coupon-use-management")
    public ModelAndView list(HttpServletRequest request, Page page) {
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-use/coupon-use-list");
        
        //列出全部已使用優惠券
        List<Coupon> coupons = couponService.listAllUseCoupons(page);

        modelAndView.addObject("useCoupons",coupons);

        return modelAndView;
    }


    /**
     * Display the search panel
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {

        ModelAndView modelAndView = new ModelAndView("coupon/coupon-use/coupon-use-search");

        return modelAndView;
    }


    /**
     * Do search
     *
     * @param couponUseSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-coupon-use-list")
    public ModelAndView onSearch(CouponUseSearch couponUseSearch, HttpServletRequest request, Page page) {

        ModelAndView modelAndView  = new ModelAndView("coupon/coupon-use/coupon-use-list");

        couponUseSearch.setStatus(CouponStatus.USED);

        List<Coupon> coupons = couponService.searchCouponUse(couponUseSearch,page);
//
        modelAndView.addObject("useCoupons", coupons);
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));

        return modelAndView;

    }


    /**
     * 
     * @param couponId
     * @return
     */
    @RequestMapping(value = "/{couponId}/use", method = RequestMethod.GET)
    public ModelAndView use(@PathVariable("couponId") Long couponId) {
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-use/coupon-use");
        
        modelAndView.addObject("couponId", couponId);
        
        return modelAndView;
    }

    /**
     * 
     * @param couponId
     * @return
     */
    @RequestMapping( value = "/{couponId}/use", method = RequestMethod.POST)
    public @ResponseBody String onUse(@PathVariable("couponId") Long couponId) {
        
        HttpResult result = new HttpResult();
        
        try {
            Coupon coupon = couponService.load(Coupon.class, couponId);
            
            couponService.useCoupon(coupon);
            
            result.success().message("优惠券核销成功！");
        } catch(BusinessException e) {
            result.fail("500").message(e.getMessage());
        } catch(Exception e) {
            logger.error("use error", e);
            
            result.fail("500").message("优惠券核销失败！");
            
        }
        
        return JsonUtils.toJson(result);
    }

 
}
